Add fake support for turn on/off for Apple TV#5962
Conversation
When the device is "turned off", no requests are sent to the device. When the setting "start_off" is set to true, the device starts in off state.
| _LOGGER.error('failed to connect to Apple TV (%s)', str(ex)) | ||
| except asyncio.TimeoutError: | ||
| _LOGGER.warning('timed out while connecting to Apple TV') | ||
| if not self._is_off: |
There was a problem hiding this comment.
Please use a guard clause:
if self._is_off:
returnIt will save you from indentation hell 👍
| """Turn the media player on.""" | ||
| self._is_off = False | ||
| self._reset() | ||
| self.schedule_update_ha_state() |
There was a problem hiding this comment.
There is no need to call this. Your device does not overwrite should_poll to return False, so Home Assistant is polling this device, including polling it right after it calls a service.
| return new_playing.media_type != old_playing.media_type or \ | ||
| new_playing.title != old_playing.title | ||
|
|
||
| def turn_on(self): |
There was a problem hiding this comment.
Since this only sets simple variables, please turn this into it's async variant async_turn_off. Has to be a coroutine. Make sure to also rename _reset to be called _async_reset and make it a @callback (imported from ha.core)
| self._reset() | ||
| self.schedule_update_ha_state() | ||
|
|
||
| def turn_off(self): |
| SUPPORT_PLAY_MEDIA | ||
| else: | ||
| return SUPPORT_PLAY_MEDIA | ||
| features |= SUPPORT_PLAY_MEDIA |
There was a problem hiding this comment.
You can move SUPPORT_PLAY_MEDIA to be always part of features and return when _playing is None or state == STATE_IDLE
* Add fake support for turn on/off for Apple TV When the device is "turned off", no requests are sent to the device. When the setting "start_off" is set to true, the device starts in off state. * Fix async comments * Clean up supported features
Description:
Some users have reported that their Apple TVs automatically start up when Home Assistant start. This is mostly a problem when CEC is enabled since it starts other devices as well. Since I have not yet found a way to know if a device is in standby or not, this cannot easily be fixed. With this PR however, it is possible to "fake" turn off/on a device so that no requests are sent to it. This way the main issue no longer exists, but the user must "turn on" the device again to get updates in Home Assistant. This is probably as good as it gets for now.
There's also a config option so that a device can start in off mode (this must be set to True). Documentation will be written when a user has verified functionality.
Related issue (if applicable): fixes #5917
Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.github.io#<home-assistant.github.io PR number goes here>
Example entry for
configuration.yaml(if applicable):Checklist:
If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
toxrun successfully. Your PR cannot be merged unless tests passREQUIREMENTSvariable (example).requirements_all.txtby runningscript/gen_requirements_all.py..coveragerc.